package dao

import (
	log "github.com/sirupsen/logrus"
	"simulationFulu/models"
)

//插入数据
func CardAdd(card *models.Card) int64{
	affected, err := x.Insert(card)
	checkErr(err)
	return affected
}

//批量插入数据
func CardBulkAdd(cards []models.Card) int64{
	affected, err := x.Insert(cards)
	checkErr(err)
	return affected
}

//改
func CardModify(id int, card *models.Card) bool {
	affected, err := x.Id(id).Cols(
		"CardDeadline",
		"CardPwd",
		"CardNumber").Update(card)
	checkErr(err)
	if affected == 0 {
		return false
	}
	return true
}

//删
func CardRemove(id int) bool{
	card := new(models.Card)
	affected, err:=x.Id(id).Delete(card)
	checkErr(err)
	if affected == 0 {
		return false
	}
	return true
}

// 查
func CardGet(id int) *models.Card {
	card := &models.Card{Id: id}
	is, _ := x.Get(card)
	if !is {
		log.Warn("搜索结果不存在!")
		return nil
	}
	return card
}

// 通过订单号查找
func CardsByOrderId(id int64) []models.Card {
	var cards []models.Card
	err:=x.Where("OrderId=?",id).Find(&cards)
	checkErr(err)
	return cards
}
